package operation;

import Book.Book;
import Book.Booklist;

import java.util.Scanner;

public class DelOperation implements IOperation{
    private Scanner scanner;



    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书");
        System.out.println("请输入要删除的书名：");
        String name = scanner.nextLine();
        int currentsize = booklist.getUsedsize();
        int index = -1;
        //1、找到要删除的书
        for (int i = 0; i < currentsize; i++) {
            Book book1 = booklist.getBooks(i);
            if (book1.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //2、index！= -1，开始删除
        if(index == -1){
            System.out.println("没有你要删除的书");
            return;
        }
        for (int i = 0; i < currentsize - 1; i++) {//如果currentsize，下一行代码会越界
                Book book = booklist.getBooks(i+1);
                booklist.setBooks(i,book);
        }
        //当书删掉之后 需要维持usedsize
        booklist.setUsedsize(currentsize-1);
    }
}
